package com.yinkgh.mybatis.aop;

import java.util.Arrays;

import org.apache.commons.beanutils.BeanUtilsBean2;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.yinkgh.mybatis.DomainEquals;
import com.yinkgh.mybatis.common.service.BaseService;
import com.yinkgh.mybatis.model.User;
import com.yinkgh.mybatis.service.UserService;
import com.yinkgh.mybatis.view.UserView;
import com.yinkgh.util.SpringContextHolder;

/**
 * 切面aop来加一些日志处理
 * 来记录哪些字段发生了变化
 * @author yinlong
 *
 */
@Component
@Aspect
@Order(1)
public class LogAspect {
	
	private static Logger logger;

//	在controller包或其子包中定义的任意方法的执行
//	@Pointcut("execution(* com.yinkgh.mybatis.controller..*.*(..))")  
	@Pointcut("execution(* com.yinkgh.mybatis.controller..*.doAdd(..)) or execution(* com.yinkgh.mybatis.controller..*.doDelete(..))  or execution(* com.yinkgh.mybatis.controller..*.doUpdate(..))")  
    private void anyMethod(){}//定义一个切入点 
	
	
//	@Before("anyMethod()")  
//    public void doAccessCheck(){  
//        System.out.println("前置通知");  
//    } 
	
	@Around("anyMethod()")  
//	@Around("execution(* com.yinkgh.mybatis.controller..*.*(..))")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable{  
        Object object = pjp.proceed();//执行该方法  
        Class clazz = pjp.getTarget().getClass();
        logger = Logger.getLogger(clazz);
//        logger.info(Arrays.toString(pjp.getArgs()));
        
        try {
			Object[] objs = pjp.getArgs();
			if  (objs != null && objs.length > 0) {
				for (Object param : objs) {
					if (param instanceof UserView) {
						UserView source = new UserView();
						BeanUtilsBean2.getInstance().copyProperties(source, (UserView) param);
						//获取参数类型
						UserView target = (UserView) param;
						target.setPassword("1234563333");
						target.setUsername("3332");
						//判断参数变化
						DomainEquals.domainEquals(source, target);
						//通过主键查询数据
						
						getByKey(source);
						
						break;
					}
				}
			}
			
			logger.info("类名："+pjp.getTarget().getClass().getName()+"调用的方法名："+pjp.getSignature().getName() );
			logger.info("获取参数"+  Arrays.toString(objs));
			//获取参数
			
			//比较实体
			
			//动态获取service
		} catch (Exception e) {
			e.printStackTrace();
		}
        
        
        return object;  
    }  
	
    /**
     * 动态获取serivce
     * @param obj
     * @return
     */
    private void getByKey(Object obj) {
    	BaseService service = null;
    	if (obj instanceof UserView) {
    		service = SpringContextHolder.getBean(UserService.class);
    		UserView userView = (UserView) obj;
    		if (userView != null) {
    			User user = (User) service.selectByPrimaryKey(userView.getId());
    			logger.info("通过注解：" + user);
    		}
    	}
    }
    
}
